<template>
    <div class="login-container">
        <div class="login-box">
            <div class="avatar-box">
                <img src="../assets/logo.png" alt="">
            </div>
            <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" class="login-form">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="el-icon-user" placeholder="用户名" maxlength="10"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" v-model="loginForm.password" prefix-icon="el-icon-lock" placeholder="密码" maxlength="20"></el-input>
                </el-form-item>
                <el-form-item class="login-btn">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="resetLoginForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    name: 'login',
    data () {
        return {
            // 表单数据绑定对象
            loginForm: {
                username: 'admin',
                passowrd: ''
            },
            // 表单验证规则
            loginFormRules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        // 登录
        login () {
            this.$refs.loginFormRef.validate(async valid => {
                if (!valid) return
                const { data: res } = await this.$http.post('login', this.loginForm)
                // console.log(res)
                if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
                this.$message.success(res.meta.msg)
                window.sessionStorage.setItem('token', res.data.token)
                this.$router.push('/home')
            })
        },
        // 表单重置
        resetLoginForm () {
            this.$refs.loginFormRef.resetFields()
        }
    }
}
</script>

<style lang="less" scoped>
.login-container {
    height: 100%;
    background-color: @bg-color-login;
}
.login-box {
    position: absolute;
    width: 450px;
    height: 300px;
    background-color: @white;
    border-radius: 5px;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .avatar-box {
        position: absolute;
        left: 50%;
        top: 0;
        width: 120px;
        height: 120px;
        padding: 10px;
        border: 1px solid @gray;
        background-color: @white;
        border-radius: 100%;
        box-shadow: 0 0 10px #ddd;
        transform: translate(-50%, -50%);
        img {
            width: 120px;
            height: 120px;
            max-width: 100%;
            border-radius: 100%;
            background-color: @gray;
        }
    }
    .login-form {
        position: absolute;
        width: 100%;
        left: 0;
        bottom: 0;
        padding: 10px 30px;
        box-sizing: border-box;
    }
}
</style>
